/**
 * Created with IntelliJ IDEA.
 * Description:两数相加
 * User: 86183
 * Date: 2023-05-03
 * Time: 7:33
 */


class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Task01 {
    public ListNode addTwoNumbers(ListNode l1,ListNode l2){
        ListNode pHead = new ListNode(-1);
        ListNode cur = pHead;
        int flag = 0;
        while (l1 != null || l2 != null){
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + flag;
            flag = sum / 10;
            cur.next = new ListNode(sum%10);
            cur = cur.next;
            if(l1 != null){
                l1 = l1.next;
            }
            if(l2 != null){
                l2 = l2.next;
            }
        }
        if(flag == 1){
            cur.next = new ListNode(flag);

        }
        return pHead.next;
    }
}
